package org.yty.travelagencymongodb.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yty.travelagencymongodb.model.Entity.ChatUser;
import org.yty.travelagencymongodb.repository.ChatUserRepository;
import org.yty.travelagencymongodb.service.ChatGroupMemberService;
import org.yty.travelagencymongodb.service.ChatUserService;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class ChatUserServiceImpl implements ChatUserService {
    private final ChatUserRepository chatUserRepository;
    private final MongoTemplate mongoTemplate;
    @Lazy
    @Autowired
    private ChatGroupMemberService chatGroupMemberService;

    @Override
    public List<ChatUser> getListByIdList(List<String> chatUserIdList) {
        return this.mongoTemplate.find(new Query(Criteria.where("id").in(chatUserIdList)), ChatUser.class);
    }

    @Override
    public ChatUser getById(String id) {
        Optional<ChatUser> option = this.chatUserRepository.findById(id);
        return option.isEmpty() ? null : option.get();
    }

    @Override
    public void deleteById(String id) {
        this.chatUserRepository.deleteById(id);
        this.chatGroupMemberService.deleteByChatUserId(id);
    }
}
